var server = require('../../../utils/server');
var app = getApp()
Page({
	add: function () {
		wx.navigateTo({
			url: '../add/add'
		});
	},
	onShow: function () {
		this.loadData();
	},
	setDefault: function (e) {
		// 设置为默认地址
		var that = this;
		// 取得id
		var index = parseInt(e.currentTarget.dataset.index);
    let _params={
      address_id:index,
      username: getApp().globalData.username,
      token: getApp().globalData.token
    };

    // server.postJSON('/person/cahngdefault',_params,(res)=>{
    //   // console.log(res);
    //   that.loadData()
    // })	
    server.send('/person/cahngdefault','POST',_params).then(res=>{
      that.loadData()
    })
	},
	edit: function (e) {
		var that = this;
		// 取得id值
		var index = parseInt(e.currentTarget.dataset.index);
    var is_defau = parseInt(e.currentTarget.dataset.isdefau)
    // console.log(is_defau)
		wx.navigateTo({
      url: '../edit/index?address_id=' + index + '&is_defau=' + is_defau
		});
	},
	delete: function (e) {
		let that = this;
		// 取得下标
		let index = parseInt(e.currentTarget.dataset.index);
    // console.log(index)
		// 找到当前地址AVObject对象
		// var address = that.data.addressObjects[index];
		// var user_id = getApp().globalData.userInfo.user_id
		// var address_id = address.address_id;
		// 给出确认提示框
		wx.showModal({
			title: '确认',
			content: '要删除这个地址吗？',
			success: function(res) {
				if (res.confirm) {
            let _params={
              username: getApp().globalData.username,
              token: getApp().globalData.token,
              address_id:index
            }
          // server.postJSON('/person/deleteaddress',_params,function(res){
          //   // console.log(res)
          //       wx.showToast({
          //                 title: res.data.msg,
          //                 icon: 'success',
          //                 duration: 2000
          //               });
                 
				  //   that.loadData();

					// 	})
          server.send('/person/deleteaddress','POST',_params).then(res=>{
                wx.showToast({
                      title: res.msg,
                      icon: 'success',
                      duration: 2000
                    });
                that.loadData();
          })

				}
			}
		})
		
	},
	loadData: function () {
		// 加载网络数据，获取地址列表
		var that = this;
    let _params={
      username: getApp().globalData.username,
      token: getApp().globalData.token
    };
		
    // server.postJSON('/person/address',_params,function(res){
		// 	var addressList = res.data.list;
    //   console.log(res);
		// 	that.setData({
		// 		addressObjects: addressList
		// 	});
		// });
    server.send('/person/address','POST',_params).then(res=>{
      var addressList = res.list;
        that.setData({
          addressObjects: addressList
        });
    })
	}
})